package com.gxgeek.javabasic.activemq.queue;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by gx on 2017/7/13.
 */
@Slf4j
public class AppProducer {

    private static final String url = "tcp://127.0.0.1:61616";
    private static final String qururName = "queue-test";

    public static void main(String[] args) throws JMSException {
        //创建连接工厂

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

        //1.创建连接
        Connection connection = connectionFactory.createConnection();
        //2.启动连接
        connection.start();
//            3.                                       是否使用事务           应答模式
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //4.创建目标
        Destination destination = session.createQueue(qururName);


        //5.创建生产者
//        MessageConsumer consumer = session.createConsumer(destination);
        MessageProducer producer = session.createProducer(destination);
        for (int i = 0 ;i<100;i++){
            // 创建消息
            TextMessage textMessage = session.createTextMessage("test:"+i);
            //发送消息
            producer.send(textMessage);
            log.info("消息发送成功，消息---->{}","test:"+i);
        }

//        最后关闭连接
        session.close();
        connection.close();

    }
}
